package com.practice.concurrency;

public class ThreadLocalFiled {

	public static class LocalRunnable implements Runnable {
		private Integer local = 0;
		private volatile Integer volatileLocal = 0;
		private ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
		
		@Override
		public void run() {
			String threadName = Thread.currentThread().getName();
			
			int number = (int)(Math.random() * 100D); 
			local = number;
			volatileLocal = number;
			threadLocal.set(number); 

			System.out.println(threadName + " local: " + local);
			System.out.println(threadName + " volatile local: " + volatileLocal);
			System.out.println(threadName + " thread local: " + threadLocal.get());
			System.out.println();
			
			try {
				Thread.sleep(2000);
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}

			System.out.println(threadName + " local: " + local);
			System.out.println(threadName + " volatile local: " + volatileLocal);
			System.out.println(threadName + " thread local: " + threadLocal.get());
			System.out.println();
		}
	}
	
	public static void main(String[] args) {
		LocalRunnable sharedRunnable = new LocalRunnable();
		
		Thread thread1 = new Thread(sharedRunnable);
		Thread thread2 = new Thread(sharedRunnable);
		
		thread1.start();
		thread2.start();
		
		try {
			thread1.join();
		} catch (InterruptedException ex) {
			ex.printStackTrace();
		}
		try {
			thread2.join();
		} catch (InterruptedException ex) {
			ex.printStackTrace();
		}
	}

}
